//
//  account.swift
//  WEIBO
//
//  Created by 李龙 on 15/6/26.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit

class account: NSObject,NSCoding {
    var access_token: String
    var expires_in: NSTimeInterval
    var uid: String
    var expires_data: NSDate
    var name: String?
    var avatar_large:String?
    
    init(dict:[String : AnyObject])
    {
        access_token = dict["access_token"] as! String
        expires_in = dict["expires_in"] as! NSTimeInterval
        expires_data = NSDate(timeIntervalSinceNow:expires_in)
        uid  = dict["uid"] as! String
    }
    func loadUserInfo(finished:(userAccount:account?,error:NSError?)->())
    {
        let pramas = ["access_token":access_token,"uid":uid]
        NetWebWorking.sharedNetWorkingTools().GET("2/users/show.json", parameters: pramas, success: { (_, JSON) -> Void in
            let dict = JSON as! [String:AnyObject]
            self.name = dict["name"] as? String
            self.avatar_large = dict["avatar_large"] as? String
            self.saveAccount()
            finished(userAccount: self, error: nil)
            }) { (_, error) -> Void in
                finished(userAccount: nil, error: error)
        }
    }
    override var description: String {
    let properties = ["access_token","expires_in","uid","name","avatar_large"]
        
        return "\(self.dictionaryWithValuesForKeys(properties))"
    }
    static let accountPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.UserDomainMask , true).last!.stringByAppendingPathComponent("account.plist")
    func saveAccount()
    {
        print(account.accountPath)
        NSKeyedArchiver.archiveRootObject(self, toFile: account.accountPath)
    }
    class func loadAccount() ->account?
    {
        print("加载用户信息")
        if let userAccount = NSKeyedUnarchiver.unarchiveObjectWithFile(account.accountPath) as? account
        {
            if userAccount.expires_data.compare(NSDate()) == NSComparisonResult.OrderedDescending
            {
            return userAccount
            }
        }
        return nil
    }
    func encodeWithCoder(aCoder: NSCoder)
    {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(expires_data, forKey: "expires_data")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    required init?(coder aDecoder: NSCoder)
    {
        access_token = aDecoder.decodeObjectForKey("access_token") as! String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        uid = aDecoder.decodeObjectForKey("uid")as! String
        expires_data = aDecoder.decodeObjectForKey("expires_data") as! NSDate
        name = aDecoder.decodeObjectForKey("name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
}
